﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MailClient;
using Microsoft.Exchange.WebServices.Authentication;
using nBayes;
using MailClient.Data.IndexTables;

namespace MailClient.Data
{
	public enum JobType { Unknown = 0, Store = 1, Person = 2 }
	/// <summary>
	/// Each email is a job.
	/// </summary>
	class Job : command, IJob
	{
		// TODO: discover the sender of the email. any operation must be constrained by who the sender is.
		// verify the sender.  check that the from email address matches the in-reply-to address.
		// Per RFC 3051 we are supposed to reply only to the in-reply-to field since the from field can differ.
		// If the two fields relate then i'm mroe certain that the person? who sent the email is the
		// person i should reply to and is not spoofing me.
		// Jobs initially try to determine the proper system command, if any
		// Once the command is figured out the subsequent commands need to call the procedures
		// on the job object to process the query string.
		// for instance the help command doesn't need to process a subject anymore than to get a supplemental command
		// however the Contacts command will need to process the subject and make guesses if the items
		// are stores or persons.
		private string _query;
		private MailMessage _msg;
		private Commands.ICommand _C;

		public Job(MailMessage msg)
		{
			_msg = msg;
			_query = msg.Subject;
			Process();
		}

		public JobType Type { get; set; }
		public string TestString { get { return _query; } set { _query = value; } }
		public Commands.ICommand GetCommand { get { return _C; } }
		public string ReplyTo { get; set; }
		public string MessageBody { get { return _msg.Body; } }
		//TODO: fix this
		public DateTime DateReceived { get { return DateTime.Today; } }

		private void Process()
		{
			// implement procedure for discovering the related job
			// there isn't anything on the InReply To field - why? i don't know yet
			var Return_Path = _msg.Sender;
			var replytogroup = _msg.ReplyToList;
			ReplyTo = _msg.From.Address;

			// Parse subject line for commands
			try { _C = TryParse(ref _query); }
			catch
			{ // handle the error, for now rethrow
				throw;

			}
			//if (_C == null)
			//{
			//    try { _C = TryParse(ref _msg.TextData); }
			//    catch
			//    { // handle the error, for rethrow
			//        throw;
			//    }
			//}
			Type = GetJobType(_query);
		}

		public JobType GetJobType(string s)
		{
			Analyzer analyzer = new Analyzer();
			Entry document = Entry.FromString(s);
			CategorizationResult result = analyzer.Categorize(
				document, PersonIndex.Index, StoreIndex.Index);

			switch (result)
			{
				case CategorizationResult.First:
					// a person
					PersonIndex.Index.Add(document);
					return Type = JobType.Person;

				case CategorizationResult.Second:
					//  a store
					StoreIndex.Index.Add(document);
					return Type = JobType.Store;

				default:
					return Type = JobType.Unknown;
					// don't know

			}
		}

	}
}
